<?php
/**
 * @file commentreply.php
 * @author LJ (liangjian@haibao.com)
 * @date 2016年1月5日 上午10:41:34
 * @brief 
 */
namespace haibao\app\data;

class CommentReply extends BaseMysql{
    
    public function __construct(){
        parent::__construct('\haibao\app\model\data\CommentReply');
    }
    
    /**
     * 记录回复评论
     */
    public function saveCommentReply($model){
        $this->add($model);
    }
    
    /**
     * 根据用户Id修改所有评论回复消息状态为已读
     */
    public function setReadReplyByUserId($userId){
        $this->query('update CommentReply set Status=%s where ReceiveUserId=%s', array(
            \haibao\app\model\data\CommentReply::STATUS_YES, $userId
        ));
    }
    
    /**
     * 修改回复评论状态
     */
    public function setCommentReplyStatus($idArr, $status, $userId = null){
        if(!$idArr || empty($status)){
            return false;
        }
        $str = preg_replace('/(\d+)/', '%s', implode(',', $idArr));
        $param = $idArr;
        array_unshift($param, $status);
        $sql = "update CommentReply set Status=%s where Id in ($str)";
        if($userId){
            $sql .= ' and ReceiveUserId=%s';
            array_push($param, $userId);
        }
        $this->query($sql, $param);
    }
    
    /**
     * 根据评论Id删除回复评论
     */
    public function deleteReplyByCommentId($commentId){
        $this->query('update CommentReply set Status=%s where CommentId=%s', array(
            \haibao\app\model\data\CommentReply::STATUS_DELETE, $commentId
        ));
    }
    
    /**
     * 查询未读回复品滚数量
     */
    public function getUnreadReplyCount($userId){
        $result = $this->query('select count(*) as count from CommentReply where ReceiveUserId=%s and Status=%s', array(
            $userId, \haibao\app\model\data\CommentReply::STATUS_NO
        ));
        $unReadCount = 0;
        while($result->fetch_assoc()){
            $unReadCount = $result->getData('count');
        }
        return $unReadCount;
    }
}